package com.rongke.web.thirdapi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rongke.utils.RandomUtils;
import com.rongke.web.thirdapi.model.OptionSymbolRequest;
import com.rongke.web.thirdapi.model.PriceLineRequest;
import com.rongke.web.thirdapi.model.QuoteOptionRequest;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.List;

public class UdSymbol {

    public static String httpPostWithJSON(String url,String jsonParams) throws Exception {

        HttpPost httpPost = new HttpPost(url);
        CloseableHttpClient client = HttpClients.createDefault();
        String respContent = null;

//        json方式

        StringEntity entity = new StringEntity(jsonParams,"utf-8");//解决中文乱码问题
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        System.out.println();


//        表单方式
//        List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
//        pairList.add(new BasicNameValuePair("name", "admin"));
//        pairList.add(new BasicNameValuePair("pass", "123456"));
//        httpPost.setEntity(new UrlEncodedFormEntity(pairList, "utf-8"));


        HttpResponse resp = client.execute(httpPost);
        if(resp.getStatusLine().getStatusCode() == 200) {
            HttpEntity he = resp.getEntity();
            respContent = EntityUtils.toString(he,"UTF-8");
        }
        return respContent;
    }



    public String getPriceLine(PriceLineRequest priceLineRequest){
        String url = "http://47.96.27.23:31009/api/v1/PriceLine";
        String result = "";
        String jsonString = JSONObject.toJSONString(priceLineRequest);
        try {
            result = httpPostWithJSON(url,jsonString);
            System.out.println("最新价格数据:"+result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /*
    *@备注：获取期权最新行情
     */
    public String getQuoteOption(List<QuoteOptionRequest> quoteOptionRequests){
        String url = "http://47.96.27.23:31009/api/v1/QuoteOption";
        String result = "";
//        UdSymbolCollectionRequst udSymbolCollectionRequst = new UdSymbolCollectionRequst();
//        udSymbolCollectionRequst.setReq_id(123);
//        udSymbolCollectionRequst.setExch_id("SFE");
        String jsonParams = JSONArray.toJSONString(quoteOptionRequests);
        try {
            result = httpPostWithJSON(url, jsonParams);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public String  getOptionSymbolCollection() {
        /*
         *1.4.	查询期权信息
         */
/*       String url = "http://47.96.27.23:31004/api/v1/QuoteOption";
        QuoteOptionRequest quoteOptionRequest = new QuoteOptionRequest("96","0","1");
        List<QuoteOptionRequest> quoteOptionRequests = new ArrayList<>();
        quoteOptionRequests.add(quoteOptionRequest);
        String jsonParams = JSONArray.toJSONString(quoteOptionRequests);*/
/*        String url = "http://47.96.27.23:31004/api/v1/UdSymbolCollection";
        UdSymbolCollectionRequst udSymbolCollectionRequst = new UdSymbolCollectionRequst();
        udSymbolCollectionRequst.setReq_id(123);
        udSymbolCollectionRequst.setExch_id("SFE");
        String jsonParams = JSONArray.toJSONString(udSymbolCollectionRequst);*/
        String url = "http://47.96.27.23:31009/api/v1/OptionSymbolCollection";
        String result = "";
        OptionSymbolRequest optionSymbolRequest = new OptionSymbolRequest();
        optionSymbolRequest.setReq_id(Integer.parseInt(RandomUtils.randomString(5)));
        optionSymbolRequest.setExch_id("SFE");
        String jsonParams = JSONArray.toJSONString(optionSymbolRequest);
        System.out.println(jsonParams);
        try {
            result = httpPostWithJSON(url, jsonParams);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    /*    String url = "http://47.98.51.74:31004/api/v1/OptionSymbolCollection";
        List<NameValuePair> nvp = new ArrayList<NameValuePair>();
        nvp.add(new BasicNameValuePair("req_id","321"));
        nvp.add(new BasicNameValuePair("Option_category_id","1"));
        nvp.add(new BasicNameValuePair("exch_id","SFE"));
        ThirdApiUtil thirdApiUtil = new ThirdApiUtil();
        String post = thirdApiUtil.httpPost(url, nvp);*/

 /*       String url = "http://47.98.51.74:31004/api/v1/UdSymbolCollection";
        List<NameValuePair> nvp = new ArrayList<NameValuePair>();
        nvp.add(new BasicNameValuePair("req_id","3432"));
        nvp.add(new BasicNameValuePair("Derivative_flag","1"));
        nvp.add(new BasicNameValuePair("exch_id","SFE"));
        ThirdApiUtil thirdApiUtil= new ThirdApiUtil();
        String s = thirdApiUtil.httpPost(url, nvp);
        System.out.println(11111+s);*/


    }


}
